## An Experimental Analysis of the Prize–Probability Tradeoff in Stopping Problems
# Yair Antler and Ayala Arad (Tel Aviv University)
# 4.1 Alternative theory-based explanations: 
# Footnote 15 - Placebo Test on randomly generated data

# Clear Global Environment and set working directory
rm(list=ls()) 

# Set working directory 
setwd("YOUR_FOLDER_PATH/stopping_rules_replication_folder")

## Package Install (install prior to load)
# install.packages("tidyverse")
# install.packages("lubridate")
# install.packages("rio")
# install.packages("bbmle")

# Load Packages 
library(tidyverse)
library(lubridate)
library(rio)
library(bbmle)

# loading function library 
source("Replication_scripts/stopping_rules_func_library/MaxLike_func_lib_script.R")

# Load Data
df <- import("Data/stopping_rules_data.csv")

#________________________________________________________________________________________________________________________________________________#

## leave-one-out cross validation exercise on randomly generated data

# Run 1

# Data generation
studentID = unique(df$studentID)
n = length(studentID)*36
set.seed(20); rand_gen_data1 <- data.frame(studentID = rep(studentID, each = 36), 
                                           qnum      = rep(1:36, length(studentID)),
                                           choice    = sample(1:5, n, replace = TRUE))
rand_gen_data1 <- cbind(rand_gen_data1, df[-c(1:3)])

# Two-Stage Qualitative Tradeoff Resolution - 2S-QTR Model
LOO_CV_2sQTR_full_random_data1 <- MaxLikeSR_2sQTR_LOO_crossVal(data=rand_gen_data1, sub_sample="full",
                                                               init=list(epsilon=0.4, alpha=0.4, beta=0.4))

# Disappointment Aversion
LOO_CV_disapAver_full_random_data1 <- MaxLikeSR_disapAver_LOO_crossVal(data=rand_gen_data1, sub_sample="full", init=list(beta=2))

# Cumulative Prospect Theory (3 parameter model)
LOO_CV_CPT_3param_full_random_data1 <- MaxLikeSR_CPT_3param_LOO_crossVal(data=rand_gen_data1, sub_sample="full",
                                                                         init=list(loss=2, delta = 0.2, alpha = 0.2))


# Run 2

# Data generation
studentID = unique(df$studentID)
n = length(studentID)*36
set.seed(21); rand_gen_data2 <- data.frame(studentID = rep(studentID, each = 36), 
                                           qnum      = rep(1:36, length(studentID)),
                                           choice    = sample(1:5, n, replace = TRUE))
rand_gen_data2 <- cbind(rand_gen_data2, df[-c(1:3)])



# Two-Stage Qualitative Tradeoff Resolution - 2S-QTR Model
LOO_CV_2sQTR_full_random_data2 <- MaxLikeSR_2sQTR_LOO_crossVal(data=rand_gen_data2, sub_sample="full",
                                                               init=list(epsilon=0.4, alpha=0.4, beta=0.4))

# Disappointment Aversion
LOO_CV_disapAver_full_random_data2 <- MaxLikeSR_disapAver_LOO_crossVal(data=rand_gen_data2, sub_sample="full", init=list(beta=2))

# Cumulative Prospect Theory (3 parameter model)
LOO_CV_CPT_3param_full_random_data2 <- MaxLikeSR_CPT_3param_LOO_crossVal(data=rand_gen_data2, sub_sample="full",
                                                                         init=list(loss=2, delta = 0.4, alpha = 0.2))

# exporting results to excel
export(list(Run1_2sQTR =      LOO_CV_2sQTR_full_random_data1$summary,
            Run2_2sQTR =      LOO_CV_2sQTR_full_random_data2$summary,
            Run1_disapAver =  LOO_CV_disapAver_full_random_data1$summary,
            Run2_disapAver =  LOO_CV_disapAver_full_random_data2$summary,
            Run1_CPT_3param = LOO_CV_CPT_3param_full_random_data1$summary,
            Run2_CPT_3param = LOO_CV_CPT_3param_full_random_data2$summary),
       "Stopping_rules_LOOCV_random_data_results_summary.xlsx")






